﻿using Oplug.EntityFramework;
using Oplug.EntityFramework.RossLive;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Oplug.Services
{
    public class EmployeeService : IDisposable
    {
        public OplugLiveDbContext dbContext;
        public IRepository<Employee, int> repository;
        public EmployeeService(string nameOrConnectionString)
        {
            dbContext = new OplugLiveDbContext(nameOrConnectionString);
            repository = new Repository<Employee, int>(dbContext);
        }

        public List<Employee> GetEmps(string keywords = "")
        {
            var result = repository.GetAllList().ToList();
            if (!string.IsNullOrEmpty(keywords))
            {
                result = result.Where(o => o.EmpID == keywords || o.EmpName.Contains(keywords)).ToList();
            }
            return result;
        }
        public List<string> GetDepts(string keywords = "")
        {
            List<string> result = new List<string>();
            var items = repository.GetAllList().Select(o => new { o.DeptName }).Distinct().ToList();
            if (!string.IsNullOrEmpty(keywords))
            {
                result = items.Where(o => o.DeptName.Contains(keywords)).Select(o => o.DeptName).ToList();
            }else
            {
                result = items.Select(o => o.DeptName).ToList();
            }
            return result;
        }
        public void Dispose()
        {
            dbContext.Dispose();
        }
    }
}
